package com.heo.web.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class BackAES {

	/**
	 * 加密解决算法
	 */
	private static BASE64Encoder encoder = new BASE64Encoder();
	private static BASE64Decoder decoder = new BASE64Decoder();
	private static String WAYS = "AES";
	private static String MODE = "";
	private static String ModeCode = "PKCS5Padding";
	private static int pwdLenght = 16;

	public static String selectMod() {
		MODE = WAYS + "/ECB/" + ModeCode;
		return MODE;

	}

	/******************************** 方法一，密匙必须为16位 **********************************************/
	// 加密
	public static String encrypt(String sSrc, String sKey) throws Exception {
		sKey = toMakekey(sKey, pwdLenght);
		Cipher cipher = Cipher.getInstance(selectMod());
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, WAYS);
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
		return encoder.encode(encrypted);// 此处使用BASE64做转码。
	}

	// 解密
	public static String decrypt(String sSrc, String sKey) throws Exception {
		sKey = toMakekey(sKey, pwdLenght);
		try {
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, WAYS);
			Cipher cipher = Cipher.getInstance(selectMod());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = decoder.decodeBuffer(sSrc);// 先用base64解密
			byte[] original = cipher.doFinal(encrypted1);
			return new String(original, "utf-8");
		} catch (Exception ex) {
			return null;
		}
	}

	// key
	public static String toMakekey(String str, int strLength) {
		int i = 0;
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer buffer = new StringBuffer();
				buffer.append(str).append(i);
				str = buffer.toString();
				strLen = str.length();
				i++;
				if (i == 10) {
					i--;
				}
			}
		}
		return str;
	}

	/*********************************** 第二种 ***********************************************/

	public static byte[] newencrypt(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建AES加密编码器
			byte[] byteContent = content.getBytes("UTF-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化AES加密
			byte[] result = cipher.doFinal(byteContent);
			return result; // AES加密结果
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * @param content 待解密内容,格式为byte数组
	 * 
	 * @param password AES解密使用的密钥
	 * 
	 * @return
	 */
	public static byte[] newdecrypt(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建AES加密编码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化AES加密
			byte[] result = cipher.doFinal(content);
			return result; // 得到AES解密结果
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
